<?php

namespace Xrfg\Api\Trading\Spot\Console;

use Illuminate\Console\Command;

class SupervisorConfig extends Command
{
    public $signature = 'supervisor:config {symbol}';

    public $description = '生成 supervisor 配置文件';

    public function handle()
    {
        $symbol = $this->argument('symbol');
        $name = str_replace('/', '_', strtolower($symbol));
        $path = base_path();
        $configPath = '/etc/supervisor/conf.d';

        $stub = file_get_contents(__DIR__.'/../../config/supervisor.stub');

        $this->config($stub, $name, $path, $configPath, $symbol, 'match');
//        $this->config($stub, $name, $path, $configPath, $symbol, 'cancel');
//        $this->config($stub, $name, $path, $configPath, $symbol, 'kline');
    }

    public function config($stub, $name, $path, $configPath, $symbol, $type)
    {
        $config = str_replace(['{name}', '{path}', '{symbol}', '{type}'], [$name, $path, $symbol, $type], $stub);
        $filename = sprintf('%s/%s_%s.conf', $configPath, $type, $name);

        $this->line('生成 '.$filename.' 配置中......');
        file_put_contents($filename, $config);

        if (file_exists($filename)) {
            $this->info('生成 '.$filename.' 配置成功！');
        } else {
            $this->error('生成 '.$filename.' 配置失败！');
        }
    }
}
